<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/28 15:39
 * Desc: 会员发票
 */
class Controller_Pc_Api_V2_Member_Invoice extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Member_Invoice
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Member_Invoice
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('member_invoice', $this->request_params);

        // 服务
        $this->service = new Service_Api_Member_Invoice();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    // 查询列表
    public function action_list()
    {
        try {
            $this->validation->list()->check();

            $this->result['data'] = $this->service->list($this->mid, $this->request_params);

        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 查询详情
    public function action_show_info()
    {
        try {
            $this->validation->show_info()->check();

            $data = $this->service->info($this->mid, $this->request_params['id']);
            if (!$data) {
                throw new Exception(__("数据不存在"));
            }

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 新增
    public function action_insert()
    {
        try {
            $this->validation->insert()->check();

            $data = $this->service->edit($this->mid, $this->request_params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 更新
    public function action_update()
    {
        try {
            $this->validation->update()->check();

            $data = $this->service->edit($this->mid, $this->request_params);

            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 删除
    public function action_delete()
    {
        try {
            $this->validation->delete()->check();

            $bool = $this->service->delete($this->mid, $this->request_params['id']);

            if (!$bool) {
                throw new Exception(__("删除失败"));
            }

            $this->result['data'] = array();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    /**
     * @function 获取产品下单的发票选择数据
     */
    public function action_get_order_invoice()
    {

        try {
            $this->validation->get_order_invoice()->check();

            $this->result['data'] = $this->service->get_order_invoice($this->request_params['type_id']);

        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }
}
